
#include "StdAfx.h"
#include "RoomCreate_Default.h"
#include "LobbyService.h"
#include "User.h"
#include "RoomManager.h"
#include "Room.h"
#include "LobbyManager.h"
#include "board_define.h"

int RoomCreate_Default::create( const char* data, User* user, Room** room ){
	RoomManager* roomMgr = LobbyService::Instance()->getRoomManager();
	if( roomMgr->size() >= LobbyService::Instance()->getRoomMax() )
		return DEF_ERROR_ROOM_CREATE_MAX;

	if(! roomMgr->create_available() )
		return DEF_CREATE_NO_AVAILABLE;

	int result = RoomCreate_Decorator::create( data, user, room );
	if( result )
		return result;
	
	result = roomMgr->createCheck( data, user );
	if( result )
		return result;

	*room = roomMgr->create( data, user );
	if(! room )
		return DEF_ERROR_INTERNAL;
	
	user->setRoom( *room );
	
	result = roomMgr->add( *room );
	if( result ){
		user->setRoom( 0 );
		roomMgr->add_delete_room( *room );

		return DEF_ERROR_INTERNAL;
	}

	LobbyManager* channelMgr = LobbyService::Instance()->getLobbyManager();	
	channelMgr->sendNewRoom( *room, user );

	return DEFAULT_SUCCESS;
}
